/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.languageschoolpersistence.DAOImpl;


import com.pa165.languageschoolpersistence.DAO.LectureDAO;
import com.pa165.languageschoolpersistence.Exceptions.IllegalArgumentPersistenceException;
import com.pa165.languageschoolpersistence.Exceptions.ServiceFailureException;
import com.pa165.languageschoolpersistence.entities.Course;
import com.pa165.languageschoolpersistence.entities.Lector;
import com.pa165.languageschoolpersistence.entities.Lecture;
import com.pa165.languageschoolpersistence.entities.Student;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Jiri Uhlir
 */
@Repository("lectureDAO")
public class LectureDAOImpl implements LectureDAO {

    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    public Lecture add(Lecture lecture) throws ServiceFailureException {
        if (lecture == null) {
            throw new IllegalArgumentPersistenceException("Lecture is null");
        }
        if (lecture.getId() != null) {
            return null;
        }
        if (lecture.getStudents() == null) {
            throw new IllegalArgumentPersistenceException("Students is null");
        }       
        if (lecture.getCourse() == null) {
            throw new IllegalArgumentPersistenceException("Lecture is not contained within any course");
        }

        em.persist(lecture);

        return lecture;
    }

    public void remove(Lecture lecture) throws IllegalArgumentPersistenceException {
        if (lecture == null) {
            throw new IllegalArgumentPersistenceException("Lecture is null");
        }
        if (lecture.getId() == null) {
            throw new IllegalArgumentPersistenceException("Lecture not in the Database");
        }       
        if (lecture.getStudents() == null) {
            throw new IllegalArgumentPersistenceException("Students is null");
        }
        if (lecture.getCourse() == null) {
            throw new IllegalArgumentPersistenceException("Lecture is not contained within any course");
        }

        Lecture lectureToRemove = em.find(Lecture.class, lecture.getId());
        em.remove(lectureToRemove);
    }

    public Lecture modify(Lecture lecture) throws IllegalArgumentPersistenceException {
        if (lecture == null) {
            throw new IllegalArgumentPersistenceException("Lecture is null");
        }
        if (lecture.getId() == null) {
            throw new IllegalArgumentPersistenceException("Lecture not in the Database");
        }        
        if (lecture.getStudents() == null) {
            throw new IllegalArgumentPersistenceException("Students is null");
        }
        if (lecture.getCourse() == null) {
            throw new IllegalArgumentPersistenceException("Lecture is not contained within any course");
        }

        em.merge(lecture);
        return lecture;
    }

    public Lecture get(Long id) throws IllegalArgumentPersistenceException {
        if (id == null) {
            throw new IllegalArgumentPersistenceException("Id is null");
        }

        Lecture result;
        result = em.find(Lecture.class, id);
        return result;
    }

    public List<Lecture> getAll() {
        Query q;
        List<Lecture> result = null;
        result = em.createQuery("SELECT s FROM Lecture s").getResultList();
        return result;
    }

    public List<Lecture> getLectures(Lector lector) throws IllegalArgumentPersistenceException {
        if ((lector == null) || (lector.getId() == null)) {
            throw new IllegalArgumentPersistenceException("Method LectureDAOImpl.getLectures(lector) call with Null argument lector or its id:");
        }
        List<Lecture> result = null;
        result = em.createQuery("select ll from Lecture ll join fetch ll.lector l where l.id=:id ", Lecture.class).setParameter("id", lector.getId()).getResultList();
        return result;
    }

    public List<Lecture> getLectures(Course course) throws IllegalArgumentPersistenceException {
        if ((course == null) || (course.getId() == null)) {
            throw new IllegalArgumentPersistenceException("Method LectureDAOImpl.getLectures(course) call with Null argument course or its id:");
        }
        List<Lecture> result = null;
        result = em.createQuery("select ll from Lecture ll join fetch ll.course l where l.id=:id ", Lecture.class).setParameter("id", course.getId()).getResultList();
        return result;
    }

    public List<Lecture> getLectures(Student student) throws IllegalArgumentPersistenceException {
        if ((student == null) || (student.getId() == null)) {
            throw new IllegalArgumentPersistenceException("Method LectureDAOImpl.getLectures(Student student) call with Null argument student or id:");
        }
        List<Lecture> result = null;
        result = em.createQuery("select l from Lecture l join fetch l.students s where s.id=:id ", Lecture.class).setParameter("id", student.getId()).getResultList();
        return result;
    }
}
